<template>
    <div>
        <!-- <el-button @click="sliceClick" type="primary">
    {{ '视频剪切' }}
  </el-button> -->
    </div>
</template>
<script>
// import { mapState } from "vuex";
import { getFirstFrame, ffmpegSliceVideo } from "@/module/useFfmpeg";
import { formatTime } from "../utils/string";
import pubsub from "pubsub-js";
export default {
    props: ["obj"],
    data() {
        return {
            timer: [],
        };
    },
    watch: {
        "timer.length"() {
            this.sliceClick();
        },
    },
    // computed: {
    //     ...mapState({
    //         videoList: (state) => state.app.videoList,
    //         videoSelected: (state) => state.app.videoSelected,
    //         store: (state) => state.app.store,
    //     }),
    // },
    mounted() {
        pubsub.subscribe("time", (time, value) => {
            this.timer = value;
        });
    },
    methods: {
        async sliceClick() {
            const selectItem = this.obj.videoList[this.obj.videoSelected];
            // 需先上传视频
            if (!selectItem) {
                alert("请先上传视频");
                return;
            }
            const resArr = await ffmpegSliceVideo(
                selectItem.videoUrl,
                selectItem.fileName,
                formatTime(this.timer[0].startTime, true),
                formatTime(this.timer[0].endTime, true)
            );
            console.log("aaaaaaaaaaaaaaaaaaa", resArr);

            for (let index = 0; index < resArr.length; index++) {
                const item = resArr[index];
                const fileName = `video${this.obj.videoId++}`;
                const { url: coverUrl, videoInfo } = await getFirstFrame(
                    item,
                    fileName
                );
                this.obj.videoList.push({
                    videoUrl: item,
                    coverUrl,
                    fileName,
                    videoInfo,
                });
            }
        },
    },
};
</script>